package com.lf.util;

import com.lf.enums.ErrorCodeEnum;
import com.lf.exception.ApiException;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

public class Asserts {

    //******

    //判断登录账号
    public static void checkAccountOrMobile(String am, String arg) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0-9]))\\d{8}$";
        String regex1 = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";

        if (!(Pattern.matches(regex, am) || Pattern.matches(regex1, am))) {
            throw new ApiException(ErrorCodeEnum.PARAM_200021.code(),ErrorCodeEnum.PARAM_200021.msg());
        }
    }
    //判断 手机号 格式
    public static void checkMobile(String mobile, String arg) {
//        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0-9]))\\d{8}$";
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0-9])|(19[8-9]))\\d{8}$";
        if (!Pattern.matches(regex, mobile)) {
            throw new ApiException(ErrorCodeEnum.PARAM_200001.code(),ErrorCodeEnum.PARAM_200001.msg());
        }
    }
    //判断 用户名 格式
    public static void checkAccount(String account, String arg) {
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";
        if (!Pattern.matches(regex, account)) {
            throw new ApiException(ErrorCodeEnum.PARAM_200002.code(),ErrorCodeEnum.PARAM_200002.msg());
        }
    }
    public static void checkVisitAccount(String account, String arg) {
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";
        if (!Pattern.matches(regex, account)) {
            throw new ApiException(ErrorCodeEnum.PARAM_200016.code(),ErrorCodeEnum.PARAM_200016.msg());
        }
    }
    //判断 密码 格式
    public static void checkPsw(String psw, String arg) {
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";
        if (!Pattern.matches(regex, psw)) {
            throw new ApiException(ErrorCodeEnum.PARAM_200003.code(),ErrorCodeEnum.PARAM_200003.msg());
        }
    }
    //判断 身份证 格式
    public static void checkIdCard(String idCard, String arg) {
        String regex = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
        if (!Pattern.matches(regex, idCard)) {
            throw new ApiException(ErrorCodeEnum.PARAM_200004.code(),ErrorCodeEnum.PARAM_200004.msg());
        }
    }
    //判断 验证码 格式
    public static void checkVcode(String code, String arg) {
        String regex = "^[0-9]{4}$";
        if (!Pattern.matches(regex, code)) {
            throw new ApiException(ErrorCodeEnum.PARAM_200005.code(),ErrorCodeEnum.PARAM_200005.msg());
        }
    }

    public static void notBlank(String str, String args) {
        if (StringUtils.isBlank(str)) {
            throw new ApiException(ErrorCodeEnum.PARAM_200006.code(),String.format(ErrorCodeEnum.PARAM_200006.msg(), args));
        }
    }

    public static void naturalNumb(Integer val, String arg) {
        if (val == null || val < 0) {
            throw new ApiException(ErrorCodeEnum.PARAM_200006.code(),String.format(ErrorCodeEnum.PARAM_200013.msg(), arg));
        }
    }
    public static void naturalNumbFromGrade(Integer val, String arg) {
        if (val == null || val < -1) {
            throw new ApiException(ErrorCodeEnum.PARAM_200006.code(),String.format(ErrorCodeEnum.PARAM_200013.msg(), arg));
        }
    }

    /**
     * one of them is not blank
     *
     * @param args
     * @param strs
     */
    public static void leastOneNotBlank(String args, String... strs) {
        for (String str : strs) {
            if (StringUtils.isNotBlank(str)) {
                return;
            }
        }
        throw new IllegalArgumentException(String.format("[%s]不能为空", args));
    }

    public static <T> void notNull(T t, String args) {
        if (t == null) {
            throw new IllegalArgumentException(String.format("[%s]不能为空", args));
        }
    }

    public static void naturalNumber(String val, String arg) {
        if (StringUtils.isBlank(val)) {
            throw new IllegalArgumentException(String.format("%s不是有效自然数", arg));
        }
        try {
            Asserts.naturalNumber(Integer.parseInt(val), arg);
        } catch (NumberFormatException e) {
            try {
                Asserts.naturalNumber(Long.parseLong(val), arg);
            } catch (NumberFormatException e1) {
                throw new IllegalArgumentException(String.format("%s不是有效自然数", arg));
            }
        }
    }

    public static void naturalNumber(Long val, String arg) {
        if (val == null || val < 0) {
            throw new IllegalArgumentException(String.format("%s不是有效自然数", arg));
        }
    }

    public static void naturalNumber(Integer val, String arg) {
        if (val == null || val < 0) {
            throw new IllegalArgumentException(String.format("%s不是有效自然数", arg));
        }
    }

    public static void checkUserId(Integer userId, String arg) {
        if (userId == null || userId < 0) {
            throw new ApiException(ErrorCodeEnum.PARAM_200035.code(), String.format("%s错误，请重新登陆", arg));
        }
    }

    public static void checkName(String realName, String arg) {
        if (realName.isEmpty()){
            throw new ApiException(ErrorCodeEnum.PARAM_200034.code(),String.format("%s错误", arg));
        }
    }

    public static void checkCode(String code, String arg) {
        String regex = "^[0-9]{6}$";
        if (!Pattern.matches(regex, code)) {
            throw new ApiException(ErrorCodeEnum.PARAM_200033.code(),String.format("%s格式6位数字", arg));
        }

//        if (code.isEmpty() || code.length() < 6 || code.length() > 6){
//            throw new ApiException(ErrorCodeEnum.PARAM_200033.code(),String.format("%s格式6位数字", arg));
//        }

    }

    public static void checkPhone(String phone, String 用户手机号) {
    }

    public static void checkReportContent(String reportContent, String args) {

    }
}
